package com.stgy.lease.web.admin.controller.attr;

import com.stgy.lease.common.redis.RedisPrefixEnum;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.FeeKey;
import com.stgy.lease.model.domain.FeeValue;
import com.stgy.lease.web.admin.service.FeeKeyService;
import com.stgy.lease.web.admin.service.FeeValueService;
import com.stgy.lease.web.admin.vo.AttrKeyVo;
import com.stgy.lease.web.admin.vo.FeeKeyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:FeeController
 * Package:com.stgy.lease.web.admin.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/8/30 - 15:25
 */
@RestController
@RequestMapping("/admin/fee")
@Tag(name = "房间杂费管理")
public class FeeController {

    @Autowired
    private FeeKeyService feeKeyService;

    @Autowired
    private FeeValueService feeValueService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String feeKeyVoListKey = RedisPrefixEnum.STGY_ADMIN_PREFIX + "fee:key:vo:list";

    //http://localhost:5174/admin/fee/list GET
    @GetMapping("/list")
    @Operation(summary = "查询公寓杂费列表和值")
    public Result<List<FeeKeyVo>> list() {
        List<FeeKeyVo> feeKeyVos = (List<FeeKeyVo>) redisTemplate.opsForValue().get(feeKeyVoListKey);
        if (feeKeyVos == null) {
            feeKeyVos = feeKeyService.selectFeeKeyVos();
            redisTemplate.opsForValue().set(feeKeyVoListKey, feeKeyVos, 1, TimeUnit.DAYS);
        }
        return Result.ok(feeKeyVos);
    }

    //http://localhost:5174/admin/fee/value/saveOrUpdate POST
    //{id: "", name: "1", unit: "1", feeKeyId: "1", feeKeyName: "停车费"}
    @Operation(summary = "添加或修改公寓杂费列表和值")
    @PostMapping("/value/saveOrUpdate")
    public Result<String> saveOrUpdate(@RequestBody FeeValue feeValue) {//FeeValueKeyVo
        boolean b = feeKeyService.saveOrUpdateFeeValue(feeValue);
        if (b) {
            redisTemplate.delete(feeKeyVoListKey);
        }
        return Result.build(b);
    }

    //http://localhost:5174/admin/fee/value/deleteById?id=19 DELETE
    @Operation(summary = "根据id删除杂物属性值")
    @DeleteMapping("/value/deleteById")
    public Result<String> deleteById(@RequestParam("id") List<Long> ids) {
        boolean b = feeValueService.removeByIds(ids);
        if (b) {
            redisTemplate.delete(feeKeyVoListKey);
        }
        return Result.build(b);
    }

    //http://localhost:5174/admin/fee/key/saveOrUpdate POST
    @Operation(summary = "添加或修改杂物列表属性名")
    @PostMapping("/key/saveOrUpdate")
    public Result<String> saveOrUpdate(@RequestBody FeeKeyVo feeKeyVo) {
        boolean b = feeKeyService.saveOrUpdateFeeKeyVo(feeKeyVo);
        if (b) {
            redisTemplate.delete(feeKeyVoListKey);
        }
        return Result.build(b);
    }

    //http://localhost:5174/admin/fee/key/deleteById?feeKeyId=1829431587662528513 DELETE
    @Operation(summary = "根据id删除杂物列表")
    @DeleteMapping("/key/deleteById")
    public Result<String> deleteById(Long feeKeyId) {
        boolean b = feeKeyService.delectByfeeKeyId(feeKeyId);
        if (b) {
            redisTemplate.delete(feeKeyVoListKey);
        }
        return Result.build(b);
    }
}
